package com.zhumei.keyscale.util;

import java.util.concurrent.locks.*;
import com.blankj.utilcode.util.*;
import java.io.*;

public class ExeCommandUtils
{
    private String TAG;
    private boolean bRunning;
    private boolean bSynchronous;
    private BufferedReader errorResult;
    ReadWriteLock lock;
    private int m_maxTime;
    private DataOutputStream os;
    private Process process;
    private StringBuffer result;
    private BufferedReader successResult;
    
    public ExeCommandUtils() {
        this.TAG = "ExeCommandUtils";
        this.bRunning = false;
        this.lock = new ReentrantReadWriteLock();
        this.result = new StringBuffer();
        this.m_maxTime = 0;
        this.bSynchronous = true;
    }
    
    public ExeCommandUtils(final boolean bSynchronous) {
        this.TAG = "ExeCommandUtils";
        this.bRunning = false;
        this.lock = new ReentrantReadWriteLock();
        this.result = new StringBuffer();
        this.m_maxTime = 0;
        this.bSynchronous = bSynchronous;
    }
    
    public String getResult() {
        final Lock lock = this.lock.readLock();
        lock.lock();
        try {
            return new String(this.result);
        }
        finally {
            lock.unlock();
        }
    }
    
    public boolean isRunning() {
        return this.bRunning;
    }
    
    public ExeCommandUtils run(final String ex, final int maxTime) {
        final StringBuilder sb = new StringBuilder();
        sb.append("run command:");
        sb.append((String)ex);
        sb.append(",maxtime:");
        sb.append(maxTime);
        LogUtils.e(sb.toString());
        this.m_maxTime = maxTime;
        if (ex == null) {
            return this;
        }
        if (((String)ex).length() == 0) {
            return this;
        }
        while (true) {
            try {
                LogUtils.e("get su");
                this.process = Runtime.getRuntime().exec("su");
                LogUtils.e("su");
                Label_0129: {
                    break Label_0129;
                    try {
                        this.process = Runtime.getRuntime().exec("sh");
                        LogUtils.e("sh");
                        this.bRunning = true;
                        this.successResult = new BufferedReader(new InputStreamReader(this.process.getInputStream()));
                        this.errorResult = new BufferedReader(new InputStreamReader(this.process.getErrorStream()));
                        final DataOutputStream os = new DataOutputStream(this.process.getOutputStream());
                        this.os = os;
                        try {
                            os.write(((String)ex).getBytes());
                            this.os.writeBytes("\n");
                            this.os.flush();
                            this.os.writeBytes("exit\n");
                            this.os.flush();
                            this.os.close();
                            if (maxTime > 0) {
                                new Thread(new -$$Lambda$ExeCommandUtils$MTl5QHRFLiIgax49dxF3DbLHCCI(this)).start();
                            }
                            final Thread thread = new Thread(new -$$Lambda$ExeCommandUtils$Huv4tqTdyhkFll-5MK87oWa31ws(this));
                            thread.start();
                            final Thread thread2 = new Thread(new -$$Lambda$ExeCommandUtils$qDEVFsGa9Tkd8fqBoJBKgwnPVak(this));
                            thread2.start();
                            final Thread thread3 = new Thread(new -$$Lambda$ExeCommandUtils$ffaARW9OpVfmBgyFgEgu_8N7SX4(this, thread, thread2));
                            thread3.start();
                            if (this.bSynchronous) {
                                LogUtils.e("run is go to end");
                                thread3.join();
                                LogUtils.e("run is end");
                                return this;
                            }
                        }
                        catch (Exception ex2) {
                            final StringBuilder sb2 = new StringBuilder();
                            sb2.append("run command process exception:");
                            sb2.append(ex2.toString());
                            LogUtils.e(sb2.toString());
                        }
                        return this;
                    }
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                return this;
            }
            catch (Exception ex3) {
                continue;
            }
            break;
        }
    }
}
